import Vue from "vue";
import store from "../store";
import Router from "vue-router";
import vmodal from "vue-js-modal";

const Home = () => import("@/pages/home");
const frontpage = () => import("@/pages/frontpage");
const classEdit = () => import("@/pages/classes/classEdit");
const classDetail = () => import("@/pages/classes/classDetail");
const editComments = () => import("@/pages/classes/editComments");
const PersonalCenter = () => import("@/pages/PersonalCenter");
const personalcomments = () => import("@/pages/PersonalComments");
const writePersonComments = () => import("@/pages/writePersonComments");
const editPersonal = () => import("@/pages/editPersonal");
const SearchItem = () => import("@/pages/SearchItem");
const comments = () => import("@/pages/classes/comments");
Vue.use(Router);
Vue.use(vmodal);
if (window.localStorage.getItem("token")) {
  store.commit("setToken", window.localStorage.getItem("token"));
}
const routes = [
  {
    path: "/",
    component: Home,
    children: [
      {
        path: "",
        name: "frontpage",
        component: frontpage
      },
      {
        path: "classes",
        name: "classEdit",
        component: classEdit,
        meta: {
          requireAuth: true
        },
        children: [
          {
            path: "classDetail",
            name: "classDetail",
            component: classDetail
          },
          {
            path: "editComments",
            name: "editComments",
            component: editComments,
            meta: {
              requireAuth: true
            }
          },
          {
            path: "comments",
            name: "comments",
            meta: {
              requireAuth: true
            },
            component: comments
          }
        ]
      },
      {
        path: "SearchItem",
        name: "SearchItem",
        component: SearchItem
      },
      {
        path: "PersonalCenter",
        name: "PersonalCenter",
        meta: {
          requireAuth: true
        },
        component: PersonalCenter
      },
      {
        path: "personalcomments",
        name: "personalcomments",
        meta: {
          requireAuth: true
        },
        component: personalcomments
      },
      {
        path: "editPersonal",
        name: "editPersonal",
        meta: {
          requireAuth: true
        },
        component: editPersonal
      },
      {
        path: "writePersonComments",
        name: "writePersonComments",
        meta: {
          requireAuth: true
        },
        component: writePersonComments
      }
    ]
  }
];
const router = new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    if (store.state.token) {
      // 通过vuex state获取当前的token是否存在
      next();
    } else {
      try {
        localStorage.removeItem("token");
      } catch (e) {}
      next({
        path: "/",
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
    }
  } else {
    next();
  }
});
export default router;
